package com.lzs.baseruku.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzs.baseruku.service.CommodityService;
import com.lzs.baseruku.service.PurchaseDetailService;
import com.lzs.baseruku.service.PurchaseService;
import com.lzs.baseruku.service.SupplierService;
import com.lzs.entity.Commodity;
import com.lzs.entity.Purchase;
import com.lzs.entity.PurchaseDetail;
import com.lzs.entity.Supplier;
import com.lzs.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ymx
 * @since 2020-11-24
 */
@RestController
@RequestMapping("/baseruku/purchase")
@Api(value = "采购订单文档")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @ApiOperation(value = "点击结束时计算总数量")
    @GetMapping("/zonghe/{purchaseno}")
    public void zonghe(@PathVariable String purchaseno){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("purchaseno",purchaseno);
        Purchase purchase = this.purchaseService.getOne(queryWrapper);
        Long id=purchase.getId();
        QueryWrapper<PurchaseDetail> queryWrapper1=new QueryWrapper();
        queryWrapper1.eq("pid",id);
        List<PurchaseDetail> purchaseDetails = this.purchaseDetailService.list(queryWrapper1);
        int zqty=0;
        for (int i=0;i<purchaseDetails.size();i++){
            Integer qty=purchaseDetails.get(i).getQty();
            zqty=zqty+qty;
        }
        purchase.setCommoditynum(zqty);
        QueryWrapper queryWrapper2=new QueryWrapper();
        queryWrapper2.eq("id",id);
        this.purchaseService.update(purchase,queryWrapper2);
    }

    @ApiOperation(value = "点击结束修改采购订单状态")
    @GetMapping("/updatePurchaseState/{purchaseno}")
    public R updatePurchaseState(@PathVariable String purchaseno) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("purchaseno",purchaseno);
        Purchase purchase = this.purchaseService.getOne(queryWrapper);
        Long id=purchase.getId();
        purchase.setState("1");
        QueryWrapper queryWrapper1=new QueryWrapper();
        queryWrapper1.eq("id",id);
        boolean b = this.purchaseService.update(purchase, queryWrapper1);
        if (b){
            return R.ok().message("操作成功");
        }else {
            return R.error().message("操作失败");
        }
    }

    @ApiOperation(value = "根据订单编号查询订单详情")
    @GetMapping("/selectPurchaseByPurchaseno/{purchaseno}")
    public R selectPurchaseByPurchaseno(@PathVariable String purchaseno){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("purchaseno",purchaseno);
        Purchase purchase = this.purchaseService.getOne(queryWrapper);
        Long id=purchase.getId();
        QueryWrapper queryWrapper1=new QueryWrapper();
        queryWrapper1.eq("pid",id);
        List list = this.purchaseDetailService.list(queryWrapper1);
        return R.ok().data("list",list);
    }

    @ApiOperation(value = "根据订单编号查询id")
    @GetMapping("/selectIdByPurchaseNo/{purchaseno}")
    public R selectIdByPurchaseNo(@PathVariable String purchaseno){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("purchaseno",purchaseno);
        Purchase purchase = this.purchaseService.getOne(queryWrapper);
        return R.ok().data("purchase",purchase);
    }

    @ApiOperation(value = "根据所有商品编码")
    @GetMapping("/selectCommodityBar")
    public R selectCommodityBar(){
        List<Commodity> commodityBars = this.commodityService.list();
        return R.ok().data("commodityBars",commodityBars);
    }

    @ApiOperation(value = "查询所有采购订单")
    @PostMapping("/selectPurchase/{page}/{limit}")
    public R selectPurchase(@PathVariable Integer page, @PathVariable Integer limit, @RequestBody Purchase purchase){
        Page<Purchase> purchasePage=purchaseService.selectPurchase(page,limit,purchase);
        return R.ok().data("purchasePage",purchasePage);
    }

    @ApiOperation(value = "根据所有供应商")
    @GetMapping("/selectAllSupplier")
    public R selectAllSupplier(){
        List<Supplier> suppliers = this.supplierService.list();
        return R.ok().data("suppliers",suppliers);
    }

    @ApiOperation(value = "下拉所有商品")
    @GetMapping("/selectAllCommdity")
    public R selectAllCommodity(){
        List<Commodity> commodities = this.commodityService.list();
        return R.ok().data("commodities",commodities);
    }

    @ApiOperation(value = "根据ID查询采购订单")
    @GetMapping("/selectPurchaseById/{id}")
    public R selectPurchaseById(@PathVariable Long id){
        Purchase purchase = this.purchaseService.getById(id);
        if (purchase.getState().equals("2")){
            return R.error().message("此订单已审核，禁止修改");
        }else {
            return R.ok().data("purchase",purchase);
        }
    }

    @ApiOperation(value = "新增或修改采购订单")
    @PostMapping("/insertOrUpdatePurchase")
    public R insertOrUpdatePurchase(@RequestBody Purchase purchase){
            if (purchase.getId()==null){
                purchase.setState("0");
            }
            boolean b = purchaseService.saveOrUpdate(purchase);
            if (b){
                return R.ok().message("操作成功");
            }else {
                return R.error().message("操作失败");
            }
    }

    @ApiOperation(value = "根据ID删除采购订单")
    @DeleteMapping("/deletePurchase/{id}")
    public R deletePurchase(@PathVariable Long id){
        Purchase purchase = this.purchaseService.getById(id);
        if (purchase.getState().equals("2")){
            return R.error().message("此订单已审核，禁止修改");
        }else {
            try {
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("pid", id);
                this.purchaseDetailService.remove(queryWrapper);
                this.purchaseService.removeById(id);
                return R.ok().message("删除成功");
            }catch (Exception e){
                return R.error().message("删除失败");
            }
        }
    }

    @ApiOperation(value = "初始采购订单状态")
    @GetMapping("/FirstState")
    public R FirstState(@RequestBody List<Long> ids){
        return purchaseService.updateState(ids,"0");
    }

    @ApiOperation(value = "审核采购订单")
    @PostMapping("/updateState")
    public R updateState(@RequestBody List<Long> ids){
        for (int i=0;i<ids.size();i++) {
            Purchase purchase = this.purchaseService.getById(ids.get(i));
            if (purchase.getState().equals("2")) {
                return R.error().message("订单已审核，不能重复审核");
            }
            if (purchase.getState().equals("0")) {
                return R.error().message("订单未录入结束，不能审核");
            }
            if (purchase.getState().equals("3")) {
                return R.error().message("订单已接货，不能审核");
            }
        }
        return purchaseService.updateState(ids, "2");
    }

    @ApiOperation(value = "撤销审核采购订单")
    @PostMapping("/NoUpdateState")
    public R NoUpdateState(@RequestBody List<Long> ids){
        for (int i=0;i<ids.size();i++){
            Purchase purchase = this.purchaseService.getById(ids.get(i));
            if (purchase.getState().equals("1")||purchase.getState().equals("0")){
                return R.error().message("订单未审核，不能撤销审核");
            }
            if (purchase.getState().equals("3")) {
                return R.error().message("订单已接货，不能撤销审核");
            }
        }
        return purchaseService.updateState(ids,"1");
    }

    @ApiOperation(value = "查询所有采购订单详情")
    @GetMapping("/selectPurchaseDetail/{page}/{limit}/{id}")
    public R selectPurchaseDetail(@PathVariable Integer page, @PathVariable Integer limit, @PathVariable Long id){
        Page<PurchaseDetail> purchaseDetail=purchaseDetailService.lookPurchaseDetail(page,limit,id);
        return R.ok().data("purchaseDetail",purchaseDetail);
    }

    @ApiOperation(value = "添加采购订单详情")
    @PostMapping("/insertPurchaseDetail")
    public R insertPurchaseDetail(@RequestBody PurchaseDetail purchaseDetail){
        Long pid=purchaseDetail.getPid();
        String bar=purchaseDetail.getCommoditybar();

        QueryWrapper queryWrapper3=new QueryWrapper();
        queryWrapper3.eq("bar",bar);
        Commodity commodity1 = this.commodityService.getOne(queryWrapper3);
        String name=commodity1.getName();
        purchaseDetail.setCommodityname(name);

        Integer lrqty=purchaseDetail.getQty();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("bar",bar);
        Commodity commodity = this.commodityService.getOne(queryWrapper);
        Long id=commodity.getId();
        purchaseDetail.setCommodityid(id);

        QueryWrapper queryWrapper1=new QueryWrapper();
        queryWrapper1.eq("pid",pid);
        queryWrapper1.eq("commoditybar",bar);
        PurchaseDetail purchaseDetail1 = this.purchaseDetailService.getOne(queryWrapper1);
        if (purchaseDetail1!=null){
            Integer qty=purchaseDetail1.getQty();
            Integer zqty=qty+lrqty;
            purchaseDetail1.setQty(zqty);
            Long id1=purchaseDetail1.getId();
            QueryWrapper queryWrapper2=new QueryWrapper();
            queryWrapper2.eq("id",id1);
            boolean b = this.purchaseDetailService.update(purchaseDetail1, queryWrapper2);
            if (b) {
                return R.ok().message("添加成功");
            } else {
                return R.error().message("添加失败");
            }
        }else {
            boolean b = this.purchaseDetailService.save(purchaseDetail);
            if (b) {
                return R.ok().message("添加成功");
            } else {
                return R.error().message("添加失败");
            }
        }
    }
}

